<?php
/**
* @author Bimal Poudel
* @copyright since 2006, Bimal Poudel
* @package Smarty Framework
* @support http://www.odesk.com/users/~~dd91d11caed0cdce
* @contact http://www.sanjaal.com
* @company Sanjaal Corps
*/

# Created on: 2011-03-26 02:03:52 890

/**
* posts Class
*/

/**
* Operations:
*	$posts->add()
*		Adds a new record in posts
*	$posts->edit()
*		Modified a record in posts
*	$posts->delete()
*		Removes one of posts record
*	$posts->list_entries()
*		Fetches a list of posts records
*	$posts->details()
*		Fetches the details of posts
*/

class posts
extends entity
{
	/**
	* Optional Constructor: Load on demand only.
	*/
	public function __construct()
	{
		# Parent's default constructor is necessary.
		parent::__construct();

		/**
		* Set Private, Protected or Public Members
		*/
		$this->protection_code = 'e7870a68905728bba956fd9fa9cb7654'; # Some random text, valid for the entire life
		$this->table_name = 'microblog_posts'; # Name of this table/entity name
		$this->pk_column = 'post_id'; # Primary Key's Column Name
		
		# Pagination helper
		$this->total_entries_for_pagination = 0; # Number of entries: list_entries()

		/**
		* Validation fields as used in add/edit forms
		*/
		$this->fields = array(
			# Remove the columns that you do not want to use in the ADD form
			'add' => array(
				'post_title' => null,
				'post_descriptions' => null,
			),
			
			# Remove the columns that you do not want to use in the EDIT form
			'edit' => array(
				'post_title' => null,
				'post_descriptions' => null,
			),
		);
	}

	/**
	* List entries from [ posts ]
	* Column `code` signifies a protection code while deleting/editing a record
	* @param $conditions SQL Conditions
	* @return Multi-Dimensional array of entries in the list
	*/
	public function list_entries(condition $condition, $from_index=0, $per_page=50)
	{
		$crud = new crud();

		/**
		* Conditions are Compiled here so that we can manupulate them individually.
		* And make them fit for [ posts ] only.
		*/
		$conditions_compiled_AND = $crud->compile_conditions(
			$condition->get_condition('AND'),
			false, 'AND', 1
		);
		$conditions_compiled_OR = $crud->compile_conditions(
			$condition->get_condition('OR'),
			false, 'OR', 2
		);

		$from_index = (int)$from_index;
		$per_page   = (int)$per_page;
		$variable   = new variable(); # It may be necessary to read list out data of a user

		$listing_sql="
SELECT SQL_CALC_FOUND_ROWS
	e.`post_id`, # Do not remove this
	
	e.category_id,
	e.added_on,
	
	e.is_text,
	
	# Modify these columns to your own list(e.*)
	e.`post_title`,
	
	# For front end
	e.post_descriptions,
	
	MD5(CONCAT(`post_id`, '{$this->protection_code}')) `code` # Protection Code
FROM `microblog_posts` `e`
WHERE
	(
		{$conditions_compiled_AND}
	)
	AND (
		{$conditions_compiled_OR}
	)
ORDER BY
	# We assume that the sorting fields are available
	#`sink_weight` ASC,
	`post_id` DESC
LIMIT {$from_index}, {$per_page}
;";

		$this->query($listing_sql);
		$entries = $this->to_array();

		# Pagination helper: Set the number of entries
		$counter_sql="SELECT FOUND_ROWS() total;"; # Uses SQL_CALC_FOUND_ROWS from above query. So, run it immediately.
		$totals = $this->row($counter_sql);
		$this->total_entries_for_pagination = $totals['total'];

		return($entries);
	}
	
	/**
	* Details of an entity in [ posts ] for management activities
	* @param $pk integer Primary Key's value of an entity
	* @return $details Associative Array of Detailed records of an entity
	*/
	public function details($post_id=0)
	{
		$post_id = (int)$post_id;
		$details_sql="
SELECT
	e.`post_id`, # Do not remove this

	e.*, # Modify these columns,
	
	# Admin must have it to EDIT the records
	MD5(CONCAT(`post_id`, '{$this->protection_code}')) `code` # Protection Code
FROM `microblog_posts` `e`
WHERE
	`post_id` = {$post_id}
;";
		$details = $this->row($details_sql);
		return($details);
	}

	/**
	* Details of an entity in [ posts ] for public display.
	* @param $pk integer Primary Key's value of an entity
	* @return $details Associative Array of Detailed records of an entity
	*/
	public function get_details($post_id=0, $protection_code='')
	{
		$protection_code = $this->sanitize($protection_code);
		$post_id = (int)$post_id;
		$details_sql="
SELECT
	`post_id`, # Do not remove this

	e.*, # Modify these columns

	MD5(CONCAT(`post_id`, '{$this->protection_code}')) `code` # Protection Code
FROM `microblog_posts` `e`
WHERE
	`post_id` = {$post_id}
	AND e.is_active='Y'

	# Optionally validate
	AND MD5(CONCAT(`post_id`, '{$this->protection_code}')) = '{$protection_code}'
;";
		$details = $this->row($details_sql);
		return($details);
	}





	/**
	* Flag a field; dummy use; unless you use it.
	* Every method should sanitize the user input.
	* It will co-exist with the live features.
	*/
	public function flag_dummyfield($post_id=0, $protection_code='')
	{
		$protection_code = $this->sanitize($protection_code);
		$post_id = (int)$post_id;

		$flag_sql="
UPDATE `microblog_posts` SET
	# Set your flag name here
	flag_name=IF(flag_name='Y', 'N', 'Y')
WHERE
	`post_id` = {$post_id}
	
	# Don't touch the deleted flags
	AND is_active='Y'

	# Optionally validate
	AND MD5(CONCAT(`post_id`, '{$this->protection_code}')) = '{$protection_code}'
;";
		# return $this->query($flag_sql);
		return false; # dummy mode only
	}

	/**
	* Block actions: delete, disable, enable, prune, nothing
	* Perform a certain action in a group of IDs. Extend only if you need them
	*/
	public function blockaction($action='nothing', $ids=array())
	{
		$ids = array_filter($ids, array(new tools(), 'numeric_only'));
		if(!$ids)
		{
			# Filter that each IDs are numeric only
			return false;
		}
		
		switch($action)
		{
		case 'delete':
			# $this->blockaction_delete($ids);
			break;
		case 'disable':
			# $this->blockaction_disable($ids);
			break;
		case 'enable':
			# $this->blockaction_enable($ids);
			break;
		case 'prune':
			# $this->blockaction_prune($ids);
			break;
		case 'nothing':
		default:
			break;
		}

		return null;
	}

	/**
	* Welcome and ask for authentication?
	* Please extend this method according to your business logic.
	* Used right after a new posts is added.
	*/
	public function welcome_first($post_id=0)
	{
		$post_id = (int)$post_id;
		
		return true;
	}
}
?>